angular.module('mamApp').controller('albumController', ['$scope', 'AlbumManager', '$state', '$uibModal', 'ImsDialog', function ($scope, AlbumManager, $state, $uibModal, ImsDialog) {

    $scope.search = {
        pageNo: $state.params.pageNo || 1,
        pageSize: $state.params.pageSize || 10,
        reviewStatus: $state.params.reviewStatus || '0',
        keyword: $state.params.keyword
    };

    $scope.keyword = $scope.search.keyword;

    $scope.paginationConf = {
        currentPage: $state.params.pageNo || 1,
        itemsPerPage: $state.params.pageSize || 10,
        pagesLength: 9,
        search: 0,
        onChange: function () {
            $scope.search.pageNo = this.currentPage;
            $scope.search.pageSize = this.itemsPerPage;
            $state.go($state.current.name, $scope.search)
        }
    }

    $scope.searchClick = function (keyword) {
        $scope.search.pageNo=1;
        $scope.search.keyword = keyword;
        $scope.search.reviewStatus = $scope.search.reviewStatus == '0' ? null : $scope.search.reviewStatus;
        $state.go($state.current.name, $scope.search)

    }

    $scope.statusClick = function (reviewStatus) {
        $scope.search.pageNo=1;
        $scope.search.keyword = null;

        $scope.search.reviewStatus = reviewStatus;
        $state.go($state.current.name, $scope.search)

    }


    AlbumManager.albumList([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
        $scope.albums = result.data.pageList;
        $scope.paginationConf.totalItems = result.data.total;
    })

    AlbumManager.albumCount().then(function (result) {
        $scope.countMessage = result.data;
    })


    $scope.delete = function (album) {
       album.myObj = {
            "color" : "red",
        }
        ImsDialog.open({
            title: "删除确认",
            message: "您确定要删除该专辑吗？<br>(专辑下面节目同时删除)"
        }).then(function (a) {
            if (a == "ok") {
                AlbumManager.albumDelete(album.albumId).then(function (result) {
                    if (result.data.c == "s") {
                        $state.reload();
                    }
                })
            }else {
                album.myObj = {
                    "color" : "#337ab7",
                }
            }
        })
    }

    $scope.uploadProgram = function (albumId) {
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'programUpload.html',
            controller: 'programUploadController',
            resolve: {
                albumId: function () {
                    return albumId;
                }
            }
        });
        modalInstance.result.then(function (result) {
            if (result == "ok") {
                setTimeout(function () {
                    $state.reload();
                }, 100)
            }
        })
    }

    $scope.keyup=function (e,keyword) {

        if(e.key=="Enter"){
            $scope.search.pageNo=1;
            $scope.search.keyword = keyword;
            $scope.search.reviewStatus = $scope.search.reviewStatus == "null" ? null : $scope.search.reviewStatus;
            $state.go($state.current.name, $scope.search)
        }
    }



}])

